<template>
	<div class="flex_col p10">
		<div class="flex_row">
			<el-select style="width: 180px;" v-model="target">
				<el-option v-for="(item, index) in REFS" :key="index" :label="item" :value="item"></el-option>
			</el-select>
			<el-button class="ml5" type="primary" @click="onClickTest">烧录</el-button>
		</div>
	</div>
</template>

<script lang="ts" setup>
import { dialog } from '@tauri-apps/api';
import { chipFamilies, debugInfo, download } from '../service/flash';
import { runAction } from '../utils/ui';
import { ref } from 'vue';

const REFS = [
	'STM32F103C8',
	'STM32F407ZE',
	'STM32F103RE',
];

const target = ref(REFS[0]);

async function init() {
	await chipFamilies();
}

async function onClickTest() {
	await runAction('烧录', '烧录', async (d) => {
		let infos = await debugInfo();
		if (infos.length == 0) {
			throw '没有调试器';
		}
		const id = infos[0];
		const filePath = await dialog.open({
			multiple: false,
			filters: [
				{
					name: 'hex',
					extensions: ['hex']
				}
			]
		});
		if (filePath == null) {
			throw '没有选择hex文件';
		}
		const fp = filePath as string;
		await download(id, fp, target.value, d);
	})
}

</script>
